/*
 * tap
 * swipe
 * pinch
 * rotate
 */
_package("alz.gesture");

/**
 * 手势
 */
_class("Gesture", "", function(){
	this.listenForStart = true;
	this.listenForEnd = true;
	this.listenForMove = true;
	this.disableLocking = false;
	this.touches = 1;
	this._init = function(config){
		_super._init.call(this);
		config = config || {};
		for(var k in config){
			this[k] = config[k];
		}
		//this.target = Ext.getDom(this.target);
		this.listeners = {};
		// <debug>
		if(!this.target){
			throw new Error('Trying to bind a ' + this.type + ' event to element that does\'nt exist: ' + this.target);
		}
		// </debug>
		this.id = runtime._element.getUid(this.target) + '-' + this.type;
		_super._init.call(this);
		this._manager = null;
		runtime._gestureManager.register(this);
	};
	this.dispose = function(){
		this.stop();
		//this._manager.unregister(this);
		this._manager = null;
		this.target = null;
		this.listeners = null;
		_super.dispose.apply(this);
	};
	this.setManager = function(v){
		this._manager = v;
	};
	this.addListener = function(name, listener){
		//console.log("Gesture::addListener(" + name + ")");
		this.listeners[name] = this.listeners[name] || [];
		this.listeners[name].push(listener);
	};
	this.removeListener = function(name, listener){
		//console.log("Gesture::removeListener(" + name + ")");
		var listeners = this.listeners[name];
		if(listeners){
			listeners.remove(listener);
			if(listeners.length == 0){
				delete this.listeners[name];
			}
			for(var k in this.listeners){
				if(this.listeners.hasOwnProperty(k)){
					return;
				}
			}
			this.listeners = {};
		}
	};
	this.fire = function(type, ev, args){
		if(!this.disableLocking && this.isLocked(type)){
			return false;
		}
		var listeners = this.listeners[type];
		if(listeners){
			args = args || {};
			var hash = {
				"time"   : ev.timeStamp,
				"type"   : type,
				"gesture": this,
				"target" : (ev.target.nodeType == 3) ? ev.target.parentNode: ev.target
			};
			for(var k in hash){
				args[k] = hash[k];
			}
			for(var i = 0, len = listeners.length; i < len; i++){
				//[TODO]针对li里面的a的特殊处理
				//[TODO]存在两个嵌套元素同时捕获事件的问题
				var listener = listeners[i];
				if(listener.obj == ev.target || listener.obj.contains(ev.target)/* && ev.target._ptr == null*/){
					listener.wrap(ev, args);
				}
			}
		}
		return true;
	};
	this.stop = function(){
		this._manager.stopGesture(this);
	};
	this.lock = function(){
		if(!this.disableLocking){
			var locks = this._manager.locks;
			for(var i = 0, len = arguments.length; i < len; i++){
				locks[arguments[i]] = this.id;
			}
		}
	};
	this.unlock = function(){
		if(!this.disableLocking){
			var locks = this._manager.locks;
			for(var i = 0, len = arguments.length; i < len; i++){
				var k = arguments[i];
				if(locks[k] == this.id){
					delete locks[k]; 
				}
			}
		}
	};
	this.isLocked = function(type){
		var lock = this._manager.locks[type];
		return !!(lock && lock !== this.id);
	};
	this.getLockingGesture  = function(type){
		var lock = this._manager.locks[type];
		if(lock){
			return this._manager.get(lock) || null;
		}
		return null;
	};
	this.onTouchStart = function(){
	};
	this.onTouchMove = function(){
	};
	this.onTouchEnd = function(){
	};
});